
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>lzma --- 用 LZMA 算法压缩 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="zipfile --- 使用ZIP存档" href="zipfile.html" />
    <link rel="prev" title="bz2 --- 对 bzip2 压缩算法的支持" href="bz2.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/lzma.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="zipfile.html" title="zipfile --- 使用ZIP存档"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="bz2.html" title="bz2 --- 对 bzip2 压缩算法的支持"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="archiving.html" accesskey="U">数据压缩和存档</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-lzma">
<span id="lzma-compression-using-the-lzma-algorithm"></span><h1><a class="reference internal" href="#module-lzma" title="lzma: A Python wrapper for the liblzma compression library."><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code></a> --- 用 LZMA 算法压缩<a class="headerlink" href="#module-lzma" title="永久链接至标题">¶</a></h1>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
<p><strong>源代码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/lzma.py">Lib/lzma.py</a></p>
<hr class="docutils" />
<p>此模块提供了可以压缩和解压缩使用 LZMA 压缩算法的数据的类和便携函数。 其中还包含支持 <strong class="program">xz</strong> 工具所使用的 <code class="docutils literal notranslate"><span class="pre">.xz</span></code> 和旧式 <code class="docutils literal notranslate"><span class="pre">.lzma</span></code> 文件格式的文件接口，以及相应的原始压缩数据流。</p>
<p>此模块所提供的接口与 <a class="reference internal" href="bz2.html#module-bz2" title="bz2: Interfaces for bzip2 compression and decompression."><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code></a> 模块的非常类似。 但是，请注意 <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> <em>不是</em> 线程安全的，这与 <a class="reference internal" href="bz2.html#bz2.BZ2File" title="bz2.BZ2File"><code class="xref py py-class docutils literal notranslate"><span class="pre">bz2.BZ2File</span></code></a> 不同，因此如果你需要在多个线程中使用单个 <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> 实例，则需要通过锁来保护它。</p>
<dl class="exception">
<dt id="lzma.LZMAError">
<em class="property">exception </em><code class="sig-prename descclassname">lzma.</code><code class="sig-name descname">LZMAError</code><a class="headerlink" href="#lzma.LZMAError" title="永久链接至目标">¶</a></dt>
<dd><p>当在压缩或解压缩期间或是在初始化压缩器/解压缩器的状态期间发生错误时此异常会被引发。</p>
</dd></dl>

<div class="section" id="reading-and-writing-compressed-files">
<h2>读写压缩文件<a class="headerlink" href="#reading-and-writing-compressed-files" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="lzma.open">
<code class="sig-prename descclassname">lzma.</code><code class="sig-name descname">open</code><span class="sig-paren">(</span><em class="sig-param">filename</em>, <em class="sig-param">mode=&quot;rb&quot;</em>, <em class="sig-param">*</em>, <em class="sig-param">format=None</em>, <em class="sig-param">check=-1</em>, <em class="sig-param">preset=None</em>, <em class="sig-param">filters=None</em>, <em class="sig-param">encoding=None</em>, <em class="sig-param">errors=None</em>, <em class="sig-param">newline=None</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.open" title="永久链接至目标">¶</a></dt>
<dd><p>以二进制或文本模式打开 LZMA 压缩文件，返回一个 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>。</p>
<p><em>filename</em> 参数可以是一个实际的文件名（以 <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 或 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">路径类</span></a> 对象的形式给出），在此情况下会打开指定名称的文件，或者可以是一个用于读写的现有文件对象。</p>
<p><em>mode</em> 参数可以是二进制模式的 <code class="docutils literal notranslate"><span class="pre">&quot;r&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;rb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;w&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;wb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;xb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;a&quot;</span></code> 或 <code class="docutils literal notranslate"><span class="pre">&quot;ab&quot;</span></code>，或者文本模式的 <code class="docutils literal notranslate"><span class="pre">&quot;rt&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;wt&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;xt&quot;</span></code> 或 <code class="docutils literal notranslate"><span class="pre">&quot;at&quot;</span></code>。 默认值为 <code class="docutils literal notranslate"><span class="pre">&quot;rb&quot;</span></code>。</p>
<p>当打开一个文件用于读取时，<em>format</em> 和 <em>filters</em> 参数具有与 <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a> 的参数相同的含义。 在此情况下，<em>check</em> 和 <em>preset</em> 参数不应被使用。</p>
<p>当打开一个文件用于写入的，<em>format</em>, <em>check</em>, <em>preset</em> 和 <em>filters</em> 参数具有与 <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMACompressor</span></code></a> 的参数相同的含义。</p>
<p>对于二进制模式，这个函数等价于 <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> 构造器: <code class="docutils literal notranslate"><span class="pre">LZMAFile(filename,</span> <span class="pre">mode,</span> <span class="pre">...)</span></code>。 在这种情况下，不可提供 <em>encoding</em>, <em>errors</em> 和 <em>newline</em> 参数。</p>
<p>对于文本模式，将会创建一个 <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> 对象，并将它包装到一个 <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> 实例中，此实例带有指定的编码格式、错误处理行为和行结束符。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>增加了对 <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;xb&quot;</span></code> 和 <code class="docutils literal notranslate"><span class="pre">&quot;xt&quot;</span></code> 模式的支持。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
</div>
</dd></dl>

<dl class="class">
<dt id="lzma.LZMAFile">
<em class="property">class </em><code class="sig-prename descclassname">lzma.</code><code class="sig-name descname">LZMAFile</code><span class="sig-paren">(</span><em class="sig-param">filename=None</em>, <em class="sig-param">mode=&quot;r&quot;</em>, <em class="sig-param">*</em>, <em class="sig-param">format=None</em>, <em class="sig-param">check=-1</em>, <em class="sig-param">preset=None</em>, <em class="sig-param">filters=None</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMAFile" title="永久链接至目标">¶</a></dt>
<dd><p>以二进制模式打开一个 LZMA 压缩文件。</p>
<p><a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> 可以包装在一个已打开的 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> 中，或者是在给定名称的文件上直接操作。 <em>filename</em> 参数指定所包装的文件对象，或是要打开的文件名称（类型为 <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a>, <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 或 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">路径类</span></a> 对象）。 如果是包装现有的文件对象，被包装的文件在 <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> 被关闭时将不会被关闭。</p>
<p><em>mode</em> 参数可以是表示读取的 <code class="docutils literal notranslate"><span class="pre">&quot;r&quot;</span></code> (默认值)，表示覆写的 <code class="docutils literal notranslate"><span class="pre">&quot;w&quot;</span></code>，表示单独创建的 <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code>，或表示添加的 <code class="docutils literal notranslate"><span class="pre">&quot;a&quot;</span></code>。 这些模式还可以分别以 <code class="docutils literal notranslate"><span class="pre">&quot;rb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;wb&quot;</span></code>, <code class="docutils literal notranslate"><span class="pre">&quot;xb&quot;</span></code> 和 <code class="docutils literal notranslate"><span class="pre">&quot;ab&quot;</span></code> 的等价形式给出。</p>
<p>如果 <em>filename</em> 是一个文件对象（而不是实际的文件名），则 <code class="docutils literal notranslate"><span class="pre">&quot;w&quot;</span></code> 模式并不会截断文件，而会等价于 <code class="docutils literal notranslate"><span class="pre">&quot;a&quot;</span></code>。</p>
<p>当打开一个文件用于读取时，输入文件可以为多个独立压缩流的拼接。 它们会被作为单个逻辑流被透明地解码。</p>
<p>当打开一个文件用于读取时，<em>format</em> 和 <em>filters</em> 参数具有与 <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a> 的参数相同的含义。 在此情况下，<em>check</em> 和 <em>preset</em> 参数不应被使用。</p>
<p>当打开一个文件用于写入的，<em>format</em>, <em>check</em>, <em>preset</em> 和 <em>filters</em> 参数具有与 <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMACompressor</span></code></a> 的参数相同的含义。</p>
<p><a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> 支持 <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a> 所指定的所有成员，但 <code class="xref py py-meth docutils literal notranslate"><span class="pre">detach()</span></code> 和 <code class="xref py py-meth docutils literal notranslate"><span class="pre">truncate()</span></code> 除外。 并支持迭代和 <a class="reference internal" href="../reference/compound_stmts.html#with"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">with</span></code></a> 语句。</p>
<p>也提供以下方法：</p>
<dl class="method">
<dt id="lzma.LZMAFile.peek">
<code class="sig-name descname">peek</code><span class="sig-paren">(</span><em class="sig-param">size=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMAFile.peek" title="永久链接至目标">¶</a></dt>
<dd><p>返回缓冲的数据而不前移文件位置。 至少将返回一个字节的数据，除非已经到达 EOF。 实际返回的字节数不确定（会忽略 <em>size</em> 参数）。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>虽然调用 <a class="reference internal" href="#lzma.LZMAFile.peek" title="lzma.LZMAFile.peek"><code class="xref py py-meth docutils literal notranslate"><span class="pre">peek()</span></code></a> 不会改变 <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> 的文件位置，但它可能改变下层文件对象的位置（举例来说如果 <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a> 是通过传入一个文件对象作为 <em>filename</em> 的话）。</p>
</div>
</dd></dl>

<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>增加了对 <code class="docutils literal notranslate"><span class="pre">&quot;x&quot;</span></code> 和 <code class="docutils literal notranslate"><span class="pre">&quot;xb&quot;</span></code> 模式的支持。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span><a class="reference internal" href="io.html#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a> 方法现在接受 <code class="docutils literal notranslate"><span class="pre">None</span></code> 作为参数。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>接受一个 <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>。</p>
</div>
</dd></dl>

</div>
<div class="section" id="compressing-and-decompressing-data-in-memory">
<h2>在内存中压缩和解压缩数据<a class="headerlink" href="#compressing-and-decompressing-data-in-memory" title="永久链接至标题">¶</a></h2>
<dl class="class">
<dt id="lzma.LZMACompressor">
<em class="property">class </em><code class="sig-prename descclassname">lzma.</code><code class="sig-name descname">LZMACompressor</code><span class="sig-paren">(</span><em class="sig-param">format=FORMAT_XZ</em>, <em class="sig-param">check=-1</em>, <em class="sig-param">preset=None</em>, <em class="sig-param">filters=None</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMACompressor" title="永久链接至目标">¶</a></dt>
<dd><p>创建一个压缩器对象，此对象可被用来执行增量压缩。</p>
<p>压缩单个数据块的更便捷方式请参阅 <a class="reference internal" href="#lzma.compress" title="lzma.compress"><code class="xref py py-func docutils literal notranslate"><span class="pre">compress()</span></code></a>。</p>
<p><em>format</em> 参数指定应当使用哪种压缩格式。 可能的值有：</p>
<ul class="simple">
<li><dl class="simple">
<dt><code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_XZ</span></code>: <code class="docutils literal notranslate"><span class="pre">.xz</span></code> 容器格式。</dt><dd><p>这是默认格式。</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt><code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_ALONE</span></code>: 传统的 <code class="docutils literal notranslate"><span class="pre">.lzma</span></code> 容器格式。</dt><dd><p>这种格式相比 <code class="docutils literal notranslate"><span class="pre">.xz</span></code> 更为受限 -- 它不支持一致性检查或多重过滤器。</p>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt><code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code>: 原始数据流，不使用任何容器格式。</dt><dd><p>这个格式描述器不支持一致性检查，并且要求你必须指定一个自定义的过滤器链（用于压缩和解压缩）。 此外，以这种方式压缩的数据不可使用 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_AUTO</span></code> 来解压缩 (参见 <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a>)。</p>
</dd>
</dl>
</li>
</ul>
<p><em>check</em> 参数指定要包含在压缩数据中的一致性检查类型。 这种检查在解压缩时使用，以确保数据没有被破坏。 可能的值是：</p>
<ul class="simple">
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_NONE</span></code>: 没有一致性检查。 这是 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_ALONE</span></code> 和 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code> 的默认值（也是唯一可接受的值）。</p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_CRC32</span></code>: 32 位循环冗余检查。</p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_CRC64</span></code>: 64 位循环冗余检查。 这是 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_XZ</span></code> 的默认值。</p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_SHA256</span></code>: 256 位安全哈希算法。</p></li>
</ul>
<p>如果指定的检查不受支持，则会引发 <a class="reference internal" href="#lzma.LZMAError" title="lzma.LZMAError"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAError</span></code></a>。</p>
<p>压缩设置可被指定为一个预设的压缩等级（通过 <em>preset</em> 参数）或以自定义过滤器链来详细设置（通过 <em>filters</em> 参数）。</p>
<p><em>preset</em> 参数（如果提供）应当为一个 <code class="docutils literal notranslate"><span class="pre">0</span></code> 到 <code class="docutils literal notranslate"><span class="pre">9</span></code> (包括边界) 之间的整数，可以选择与常数 <code class="xref py py-const docutils literal notranslate"><span class="pre">PRESET_EXTREME</span></code> 进行 OR 运算。 如果 <em>preset</em> 和 <em>filters</em> 均未给出，则默认行为是使用 <code class="xref py py-const docutils literal notranslate"><span class="pre">PRESET_DEFAULT</span></code> (预设等级 <code class="docutils literal notranslate"><span class="pre">6</span></code>)。 更高的预设等级会产生更小的输出，但会使得压缩过程更缓慢。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>除了更加 CPU 密集，使用更高的预设等级来压缩还需要更多的内存（并产生需要更多内存来解压缩的输出）。 例如使用预设等级 <code class="docutils literal notranslate"><span class="pre">9</span></code> 时，一个 <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMACompressor</span></code></a> 对象的开销可以高达 800 MiB。 出于这样的原因，通常最好是保持使用默认预设等级。</p>
</div>
<p><em>filters</em> 参数（如果提供）应当指定一个过滤器链。 详情参见 <a class="reference internal" href="#filter-chain-specs"><span class="std std-ref">指定自定义的过滤器链</span></a>。</p>
<dl class="method">
<dt id="lzma.LZMACompressor.compress">
<code class="sig-name descname">compress</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMACompressor.compress" title="永久链接至目标">¶</a></dt>
<dd><p>压缩 <em>data</em> (一个 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> object)，返回包含针对输入的至少一部分已压缩数据的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象。 一部 <em>data</em> 可能会被放入内部缓冲区，以便用于后续的 <a class="reference internal" href="#lzma.compress" title="lzma.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compress()</span></code></a> 和 <a class="reference internal" href="#lzma.LZMACompressor.flush" title="lzma.LZMACompressor.flush"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush()</span></code></a> 调用。 返回的数据应当与之前任何 <a class="reference internal" href="#lzma.compress" title="lzma.compress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">compress()</span></code></a> 调用的输出进行拼接。</p>
</dd></dl>

<dl class="method">
<dt id="lzma.LZMACompressor.flush">
<code class="sig-name descname">flush</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMACompressor.flush" title="永久链接至目标">¶</a></dt>
<dd><p>结束压缩进程，返回包含保存在压缩器的内部缓冲区中的任意数据的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象。</p>
<p>调用此方法之后压缩器将不可再被使用。</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="lzma.LZMADecompressor">
<em class="property">class </em><code class="sig-prename descclassname">lzma.</code><code class="sig-name descname">LZMADecompressor</code><span class="sig-paren">(</span><em class="sig-param">format=FORMAT_AUTO</em>, <em class="sig-param">memlimit=None</em>, <em class="sig-param">filters=None</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMADecompressor" title="永久链接至目标">¶</a></dt>
<dd><p>创建一个压缩器对象，此对象可被用来执行增量解压缩。</p>
<p>一次性解压缩整个压缩数据流的更便捷方式请参阅 <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a>。</p>
<p><em>format</em> 参数指定应当被使用的容器格式。 默认值为 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_AUTO</span></code>，它可以解压缩 <code class="docutils literal notranslate"><span class="pre">.xz</span></code> 和 <code class="docutils literal notranslate"><span class="pre">.lzma</span></code> 文件。 其他可能的值为 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_XZ</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_ALONE</span></code> 和 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code>。</p>
<p><em>memlimit</em> 参数指定解压缩器可以使用的内存上限（字节数）。 当使用此参数时，如果不可能在给定内存上限之内解压缩输入数据则解压缩将失败并引发 <a class="reference internal" href="#lzma.LZMAError" title="lzma.LZMAError"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAError</span></code></a>。</p>
<p><em>filters</em> 参数指定用于创建被解压缩数据流的过滤器链。 此参数在 <em>format</em> 为 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code> 时要求提供，但对于其他格式不应使用。 有关过滤器链的更多信息请参阅 <a class="reference internal" href="#filter-chain-specs"><span class="std std-ref">指定自定义的过滤器链</span></a>。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>这个类不会透明地处理包含多个已压缩数据流的输入，这不同于 <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code class="xref py py-func docutils literal notranslate"><span class="pre">decompress()</span></code></a> 和 <a class="reference internal" href="#lzma.LZMAFile" title="lzma.LZMAFile"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMAFile</span></code></a>。 要通过 <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a> 来解压缩多个数据流输入，你必须为每个数据流都创建一个新的解压缩器。</p>
</div>
<dl class="method">
<dt id="lzma.LZMADecompressor.decompress">
<code class="sig-name descname">decompress</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">max_length=-1</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.LZMADecompressor.decompress" title="永久链接至目标">¶</a></dt>
<dd><p>解压缩 <em>data</em> (一个 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a>)，返回字节串形式的解压缩数据。 某些 <em>data</em> 可以在内部被缓冲，以便用于后续的 <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> 调用。 返回的数据应当与之前任何 <a class="reference internal" href="#lzma.decompress" title="lzma.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> 调用的输出进行拼接。</p>
<p>如果 <em>max_length</em> 为非负数，将返回至多 <em>max_length</em> 个字节的解压缩数据。 如果达到此限制并且可以产生后续输出，则 <a class="reference internal" href="#lzma.LZMADecompressor.needs_input" title="lzma.LZMADecompressor.needs_input"><code class="xref py py-attr docutils literal notranslate"><span class="pre">needs_input</span></code></a> 属性将被设为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。 在这种情况下，下一次 <a class="reference internal" href="#lzma.LZMADecompressor.decompress" title="lzma.LZMADecompressor.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> 调用提供的 <em>data</em> 可以为 <code class="docutils literal notranslate"><span class="pre">b''</span></code> 以获取更多的输出。</p>
<p>如果所有输入数据都已被解压缩并返回（或是因为它少于 <em>max_length</em> 个字节，或是因为 <em>max_length</em> 为负数），则 <a class="reference internal" href="#lzma.LZMADecompressor.needs_input" title="lzma.LZMADecompressor.needs_input"><code class="xref py py-attr docutils literal notranslate"><span class="pre">needs_input</span></code></a> 属性将被设为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
<p>在到达数据流末尾之后再尝试解压缩数据会引发 <cite>EOFError</cite>。 在数据流末尾之后获取的任何数据都会被忽略并存储至 <a class="reference internal" href="#lzma.LZMADecompressor.unused_data" title="lzma.LZMADecompressor.unused_data"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unused_data</span></code></a> 属性。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>添加了 <em>max_length</em> 形参。</p>
</div>
</dd></dl>

<dl class="attribute">
<dt id="lzma.LZMADecompressor.check">
<code class="sig-name descname">check</code><a class="headerlink" href="#lzma.LZMADecompressor.check" title="永久链接至目标">¶</a></dt>
<dd><p>输入流使用的一致性检查的 ID。 这可能为 <code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_UNKNOWN</span></code> 直到已解压了足够的输入数据来确定它所使用的一致性检查。</p>
</dd></dl>

<dl class="attribute">
<dt id="lzma.LZMADecompressor.eof">
<code class="sig-name descname">eof</code><a class="headerlink" href="#lzma.LZMADecompressor.eof" title="永久链接至目标">¶</a></dt>
<dd><p>若达到了数据流末尾标识符则为 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="lzma.LZMADecompressor.unused_data">
<code class="sig-name descname">unused_data</code><a class="headerlink" href="#lzma.LZMADecompressor.unused_data" title="永久链接至目标">¶</a></dt>
<dd><p>压缩数据流的末尾还有数据。</p>
<p>在达到数据流末尾之前，这个值将为 <code class="docutils literal notranslate"><span class="pre">b&quot;&quot;</span></code>。</p>
</dd></dl>

<dl class="attribute">
<dt id="lzma.LZMADecompressor.needs_input">
<code class="sig-name descname">needs_input</code><a class="headerlink" href="#lzma.LZMADecompressor.needs_input" title="永久链接至目标">¶</a></dt>
<dd><p>如果在要求新的未解压缩输入之前 <a class="reference internal" href="#lzma.LZMADecompressor.decompress" title="lzma.LZMADecompressor.decompress"><code class="xref py py-meth docutils literal notranslate"><span class="pre">decompress()</span></code></a> 方法可以提供更多的解压缩数据则为 <code class="docutils literal notranslate"><span class="pre">False</span></code>。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能.</span></p>
</div>
</dd></dl>

</dd></dl>

<dl class="function">
<dt id="lzma.compress">
<code class="sig-prename descclassname">lzma.</code><code class="sig-name descname">compress</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">format=FORMAT_XZ</em>, <em class="sig-param">check=-1</em>, <em class="sig-param">preset=None</em>, <em class="sig-param">filters=None</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.compress" title="永久链接至目标">¶</a></dt>
<dd><p>压缩 <em>data</em> (一个 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象)，返回包含压缩数据的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象。</p>
<p>参见上文的 <a class="reference internal" href="#lzma.LZMACompressor" title="lzma.LZMACompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMACompressor</span></code></a> 了解有关 <em>format</em>, <em>check</em>, <em>preset</em> 和 <em>filters</em> 参数的说明。</p>
</dd></dl>

<dl class="function">
<dt id="lzma.decompress">
<code class="sig-prename descclassname">lzma.</code><code class="sig-name descname">decompress</code><span class="sig-paren">(</span><em class="sig-param">data</em>, <em class="sig-param">format=FORMAT_AUTO</em>, <em class="sig-param">memlimit=None</em>, <em class="sig-param">filters=None</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.decompress" title="永久链接至目标">¶</a></dt>
<dd><p>解压缩 <em>data</em> (一个 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象)，返回包含解压缩数据的 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 对象。</p>
<p>如果 <em>data</em> 是多个单独压缩数据流的拼接，则解压缩所有相应数据流，并返回结果的拼接。</p>
<p>参见上文的 <a class="reference internal" href="#lzma.LZMADecompressor" title="lzma.LZMADecompressor"><code class="xref py py-class docutils literal notranslate"><span class="pre">LZMADecompressor</span></code></a> 了解有关 <em>format</em>, <em>memlimit</em> 和 <em>filters</em> 参数的说明。</p>
</dd></dl>

</div>
<div class="section" id="miscellaneous">
<h2>杂项<a class="headerlink" href="#miscellaneous" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="lzma.is_check_supported">
<code class="sig-prename descclassname">lzma.</code><code class="sig-name descname">is_check_supported</code><span class="sig-paren">(</span><em class="sig-param">check</em><span class="sig-paren">)</span><a class="headerlink" href="#lzma.is_check_supported" title="永久链接至目标">¶</a></dt>
<dd><p>如果本系统支持给定的一致性检查则返回 <code class="docutils literal notranslate"><span class="pre">True</span></code>。</p>
<p><code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_NONE</span></code> 和 <code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_CRC32</span></code> 总是受支持。 <code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_CRC64</span></code> 和 <code class="xref py py-const docutils literal notranslate"><span class="pre">CHECK_SHA256</span></code> 或许不可用，如果你正在使用基于受限制特性集编译的 <strong class="program">liblzma</strong> 版本的话。</p>
</dd></dl>

</div>
<div class="section" id="specifying-custom-filter-chains">
<span id="filter-chain-specs"></span><h2>指定自定义的过滤器链<a class="headerlink" href="#specifying-custom-filter-chains" title="永久链接至标题">¶</a></h2>
<p>过滤器链描述符是由字典组成的序列，其中每个字典包含单个过滤器的 ID 和选项。 每个字典必须包含键 <code class="docutils literal notranslate"><span class="pre">&quot;id&quot;</span></code>，并可能包含额外的键用来指定基于过滤器的选项。 有效的过滤器 ID 如下：</p>
<ul class="simple">
<li><dl class="simple">
<dt>压缩过滤器：</dt><dd><ul>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_LZMA1</span></code> (配合 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_ALONE</span></code> 使用)</p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_LZMA2</span></code> (配合 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_XZ</span></code> 和 <code class="xref py py-const docutils literal notranslate"><span class="pre">FORMAT_RAW</span></code> 使用)</p></li>
</ul>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>增量过滤器：</dt><dd><ul>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_DELTA</span></code></p></li>
</ul>
</dd>
</dl>
</li>
<li><dl class="simple">
<dt>Branch-Call-Jump (BCJ) 过滤器：</dt><dd><ul>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_X86</span></code></p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_IA64</span></code></p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_ARM</span></code></p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_ARMTHUMB</span></code></p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_POWERPC</span></code></p></li>
<li><p><code class="xref py py-const docutils literal notranslate"><span class="pre">FILTER_SPARC</span></code></p></li>
</ul>
</dd>
</dl>
</li>
</ul>
<p>一个过滤器链最多可由 4 个过滤器组成，并且不能为空。 过滤器链中的最后一个过滤器必须为压缩过滤器，其他过滤器必须为 Delta 或 BCJ 过滤器。</p>
<p>压缩过滤器支持下列选项（指定为表示过滤器的字典中的附加条目）：</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">preset</span></code>: 压缩预设选项，用于作为未显式指定的选项的默认值的来源。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">dict_size</span></code>: 以字节表示的字典大小。 这应当在 4 KiB 和 1.5 GiB 之间（包含边界）。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">lc</span></code>: 字面值上下文的位数。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">lp</span></code>: 字面值位置的比特数。 总计值 <code class="docutils literal notranslate"><span class="pre">lc</span> <span class="pre">+</span> <span class="pre">lp</span></code> 必须不大于 4。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">pb</span></code>: 位置的比特数；必须不大于 4。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">mode</span></code>: <code class="xref py py-const docutils literal notranslate"><span class="pre">MODE_FAST</span></code> 或 <code class="xref py py-const docutils literal notranslate"><span class="pre">MODE_NORMAL</span></code>。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">nice_len</span></code>: 对于一个匹配应当被视为“适宜长度”的值。 这应当小于或等于 273。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">mf</span></code>: 要使用的匹配查找器 -- <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_HC3</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_HC4</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_BT2</span></code>, <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_BT3</span></code> 或 <code class="xref py py-const docutils literal notranslate"><span class="pre">MF_BT4</span></code>。</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">depth</span></code>: 匹配查找器使用的最大查找深度。 0 (默认值) 表示基于其他过滤器选项自动选择。</p></li>
</ul>
</div></blockquote>
<p>Delta 过滤器保存字节数据之间的差值，在特定环境下可产生更具重复性的输入。 它支持一个 <code class="docutils literal notranslate"><span class="pre">dist</span></code> 选项，指明要减去的字节之间的差值大小。 默认值为 1，即相邻字节之间的差值。</p>
<p>BCJ 过滤器主要作用于机器码。 它们会转换机器码内的相对分支、调用和跳转以使用绝对寻址，其目标是提升冗余度以供压缩器利用。 这些过滤器支持一个 <code class="docutils literal notranslate"><span class="pre">start_offset</span></code> 选项，指明应当被映射到输入数据开头的地址。 默认值为 0。</p>
</div>
<div class="section" id="examples">
<h2>例子<a class="headerlink" href="#examples" title="永久链接至标题">¶</a></h2>
<p>在已压缩的数据中读取:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
<span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;file.xz&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">file_content</span> <span class="o">=</span> <span class="n">f</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
</pre></div>
</div>
<p>创建一个压缩文件:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
<span class="n">data</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;Insert Data Here&quot;</span>
<span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>
</pre></div>
</div>
<p>在内存中压缩文件:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
<span class="n">data_in</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;Insert Data Here&quot;</span>
<span class="n">data_out</span> <span class="o">=</span> <span class="n">lzma</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="n">data_in</span><span class="p">)</span>
</pre></div>
</div>
<p>增量压缩:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
<span class="n">lzc</span> <span class="o">=</span> <span class="n">lzma</span><span class="o">.</span><span class="n">LZMACompressor</span><span class="p">()</span>
<span class="n">out1</span> <span class="o">=</span> <span class="n">lzc</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Some data</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="n">out2</span> <span class="o">=</span> <span class="n">lzc</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Another piece of data</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="n">out3</span> <span class="o">=</span> <span class="n">lzc</span><span class="o">.</span><span class="n">compress</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Even more data</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
<span class="n">out4</span> <span class="o">=</span> <span class="n">lzc</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span>
<span class="c1"># Concatenate all the partial results:</span>
<span class="n">result</span> <span class="o">=</span> <span class="sa">b</span><span class="s2">&quot;&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">out1</span><span class="p">,</span> <span class="n">out2</span><span class="p">,</span> <span class="n">out3</span><span class="p">,</span> <span class="n">out4</span><span class="p">])</span>
</pre></div>
</div>
<p>写入已压缩数据到已打开的文件:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s2">&quot;wb&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;This data will not be compressed</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">)</span> <span class="k">as</span> <span class="n">lzf</span><span class="p">:</span>
        <span class="n">lzf</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;This *will* be compressed</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;Not compressed</span><span class="se">\n</span><span class="s2">&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>使用自定义过滤器链创建一个已压缩文件:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">lzma</span>
<span class="n">my_filters</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="n">lzma</span><span class="o">.</span><span class="n">FILTER_DELTA</span><span class="p">,</span> <span class="s2">&quot;dist&quot;</span><span class="p">:</span> <span class="mi">5</span><span class="p">},</span>
    <span class="p">{</span><span class="s2">&quot;id&quot;</span><span class="p">:</span> <span class="n">lzma</span><span class="o">.</span><span class="n">FILTER_LZMA2</span><span class="p">,</span> <span class="s2">&quot;preset&quot;</span><span class="p">:</span> <span class="mi">7</span> <span class="o">|</span> <span class="n">lzma</span><span class="o">.</span><span class="n">PRESET_EXTREME</span><span class="p">},</span>
<span class="p">]</span>
<span class="k">with</span> <span class="n">lzma</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s2">&quot;file.xz&quot;</span><span class="p">,</span> <span class="s2">&quot;w&quot;</span><span class="p">,</span> <span class="n">filters</span><span class="o">=</span><span class="n">my_filters</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="n">f</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="sa">b</span><span class="s2">&quot;blah blah blah&quot;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">lzma</span></code> --- 用 LZMA 算法压缩</a><ul>
<li><a class="reference internal" href="#reading-and-writing-compressed-files">读写压缩文件</a></li>
<li><a class="reference internal" href="#compressing-and-decompressing-data-in-memory">在内存中压缩和解压缩数据</a></li>
<li><a class="reference internal" href="#miscellaneous">杂项</a></li>
<li><a class="reference internal" href="#specifying-custom-filter-chains">指定自定义的过滤器链</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="bz2.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">bz2</span></code> --- 对 <strong class="program">bzip2</strong> 压缩算法的支持</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="zipfile.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">zipfile</span></code> --- 使用ZIP存档</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/lzma.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="zipfile.html" title="zipfile --- 使用ZIP存档"
             >下一页</a> |</li>
        <li class="right" >
          <a href="bz2.html" title="bz2 --- 对 bzip2 压缩算法的支持"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="archiving.html" >数据压缩和存档</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>